package com.botann.projects.utils.alioss;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.GetObjectRequest;
import org.apache.log4j.Logger;

import java.io.File;

/**
 * projects
 * AliOSSUtils
 * Created by 刘山国 on 2020/5/15.
 * Copyright (c) 2017 伯坦科技. All rights reserved
 */
public class AliOSSUtils {
    // 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建RAM账号。

    public static String accessKeyId = "LTAI4GJpd5jn8facjvHE5sAW";
    public static String accessKeySecret = "WLEXPw5UGsEN4aPh4NIudZRxIICT5j";
    public static String bucketName = "botann-app-packages";
    public static String roleArn = "acs:ram::1160060351317613:role/projects-ss";
    public static String roleSessionName = "AliyunOSSProjects";
    public static String endpoint = "oss-cn-shanghai.aliyuncs.com";
    public static String sts_endpoint = "sts.cn-shanghai.aliyuncs.com";
    public static String policy = "{\n" +
            "    \"Statement\": [\n" +
            "        {\n" +
            "            \"Action\": \"oss:*\",\n" +
            "            \"Effect\": \"Allow\",\n" +
            "            \"Resource\": \"*\"\n" +
            "        }\n" +
            "    ],\n" +
            "    \"Version\": \"1\"\n" +
            "}";


    public static boolean download(String objectName) {
        return download(objectName,objectName);
    }
    public static boolean download(String objectName, String localFileName) {
        return download(objectName,localFileName,bucketName);
    }

    public static boolean download(String objectName, String localFileName, String bucketName) {

        System.out.println(objectName);
        Object o = null;
        try {
            // 创建OSSClient实例。
            OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

            // 下载OSS文件到本地文件。如果指定的本地文件存在会覆盖，不存在则新建。
            o = ossClient.getObject(new GetObjectRequest(bucketName, objectName), new File(localFileName));

            // 关闭OSSClient。
            ossClient.shutdown();
        } catch (Exception e) {
            System.out.println(e);
        }
        return o != null;

    }

    public static void main(String[] args) {
        download("upload.js");
    }
}
